/*
 * File: vfsdriver.c
 *
 * Description: This is a driver program for testing your VFS system using an interaction script as input
 * You need to make additional calls to your respective functions as noted in the comments below
 * Make sure the output you display is exactly as per the given specifications for you. Do NOT print
 * any extra output (like debug messages) in the final version of your driver program. You can use this driver program
 * in a in incremental manner as you keep implementing one operator after another. For operators not yet implemented,
 * you can leave the output as given ("TO_BE_DONE"). So you can use this program as your "main" program for testing purposes.
 *
 * DO NOT write the full code for operators in the driver program! You must only CALL your functions from here.
 *
 * Usage: vfsdriver <scriptfilename>
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include "../include/filevfs.h"
#include "../include/createfile.h"
#include "../include/narryTree.h"
#include "../include/addfile.h"
#include "../include/vfs_errorcodes.h"

#define BUFSIZE 200
#define CMDSIZE 30
#define PARSIZE 100

void createvfs ( char *P1, int P2 );
void mountvfs ( char *P1 );
void unmountvfs ( char *P1 );
void makedir ( char *P1, char *P2 );
void deletedir ( char *P1 );
void movedir ( char *P1, char *P2 );
void listdir ( char *P1, int P2, char *P3 );
void addfile ( char *P1, char *P2, char *P3 );
void listfile ( char *P1, char *P2 );
void updatefile ( char *P1, char *P2 );
void removefile ( char *P1 );
void movefile ( char *P1, char *P2 );
void copyfile ( char *P1, char *P2 );
void exportfile ( char *P1, char *P2 );
void searchfile ( char *P1, char *P2 );
void printbst ();
void printhash ();
char vfsname[20];
int agrcnt;
void processcommand( char *command, char *P1, char *P2, char *P3 );

int main( int argc, char *argv[] )
{
	FILE *scriptfp;
	char linebuffer[BUFSIZE];
	char command[CMDSIZE], par1[PARSIZE], par2[PARSIZE], par3[PARSIZE];
	char *token;
	mount_flag=0;
	//argcnt=argc;

	if( argc != 2 ){
		fprintf(stderr,"Usage: vfsdriver <scriptfile>\n");
		return(1);
	}

	if( (scriptfp=fopen(argv[1],"r")) == NULL ){
		fprintf(stderr,"Unable to open script file: %s\n", argv[1]);
		return(2);
	}

	while( fgets(linebuffer, sizeof(linebuffer), scriptfp) != NULL ){
		/* This output is for debugging... do not uncomment in final version */
		/*
		printf("==================================================\n");
		printf("Processing: %s", linebuffer);
		printf("==================================================\n");
		*/

		/* Remove the extra newline character in the end of line */
		linebuffer[ strlen(linebuffer)-1 ] = '\0';
		
		/* Get the command and the parameters using tokenizer */
		strcpy( command, (token = strtok(linebuffer, " ")) == NULL ? "" : token );
		
		strcpy( par1, (token = strtok(NULL, " ")) == NULL ? "" : token );
		strcpy( par2, (token = strtok(NULL, " ")) == NULL ? "" : token );
		strcpy( par3, (token = strtok(NULL, " ")) == NULL ? "" : token );
		/* printf("Command:%s:p1:%s:p2:%s:p3:%s\n",command, par1, par2, par3); */
		char p1[100],p2[100],p3[100];
        int i=0;
        if(par1[strlen(par1)-1]=='/' && strlen(par1)>1)
        {
            //strncpy(p1,par1,strlen(par1)-1);    
            //p1[strlen(p1)-1]='\0';
            while(i<strlen(par1)-1)
            { p1[i]=par1[i];i++; }
            p1[i]='\0';
            strcpy(par1,p1);
        }    
        i=0;
        if(par2[strlen(par2)-1]=='/' && strlen(par2)>1)
        {
            //strncpy(p2,par2,strlen(par2)-1);
            //p2[strlen(p2)-1]='\0';
            while(i<strlen(par2)-1)
            { p2[i]=par2[i];i++; }
            p2[i]='\0';
            strcpy(par2,p2);
        }

        if(par3[strlen(par3)-1]=='/' && strlen(par3)>1)
        {
            strncpy(p3,par3,strlen(par3)-1);
            //p3[strlen(p3)-1]='\0';
            strcpy(par3,p3);
        }
        //printf("in vfsdri:%s\n",p1);
        //printf("in vfsdri:%s\n",p2);
        //printf("in vfsdri:%s\n",p3);
		processcommand( command, par1, par2, par3 );
	}
}

void processcommand( char *command, char *P1, char *P2, char *P3 )
{
	if( strcmp(command, "createvfs") == 0 ){
		int size = atoi(P2);
		createvfs (P1,size);
	}
	else if( strcmp(command, "mountvfs") == 0 )
		mountvfs (P1);
	else if( strcmp(command, "unmountvfs") == 0 )
		unmountvfs (P1);
	else if( strcmp(command, "makedir") == 0 )
		makedir (P1,P2);
	else if( strcmp(command, "deletedir") == 0 )
		deletedir (P1);
	else if( strcmp(command, "movedir") == 0 )
		movedir (P1,P2);
	else if( strcmp(command, "listdir") == 0 ){
		int flag = atoi(P2);
		listdir (P1,flag,P3);
	}
	else if( strcmp(command, "addfile") == 0 )
		addfile (P1,P2,P3);
	else if( strcmp(command, "listfile") == 0 )
		listfile (P1,P2);
	else if( strcmp(command, "updatefile") == 0 )
		updatefile (P1,P2);
	else if( strcmp(command, "removefile") == 0 )
		removefile (P1);
	else if( strcmp(command, "movefile") == 0 )
		movefile (P1,P2);
	else if( strcmp(command, "copyfile") == 0 )
		copyfile (P1,P2);
	else if( strcmp(command, "exportfile") == 0 )
		exportfile (P1,P2);
	else if( strcmp(command, "searchfile") == 0 )
		searchfile (P1,P2);
	else
		printf("Ignoring invalid command %s\n", command);
}

void createvfs ( char *P1, int P2 )
{
    /* Call the appropriate function with given arguments and display appropriate output on the screen */
    //printf("createvfs_TO_BE_DONE\n");
    char arg2;
    int exitflag=0,i=0;
    if(P2<=0)
    arg2=(char)P2;
    else
    arg2=1;
    int arg1=strcmp(P1,"");
    int b;
   // printf("len:%d",strlen(P1));
    if(strlen(P1)>30 && exitflag==0)
        {
        printf("createvfs_FAILURE ");
        printf(ERR_VFS_CREATE_05);
        exitflag=1;
        printf("\n");
        }
    //printf("arg1: %d arg2: %d \n",strlen(P1),arg2);
    if(arg1==0 || arg2==0)
    {
    
    printf("createvfs_FAILURE ");
    printf(ERR_VFS_CREATE_00);
    printf("\n");
    exitflag=1;
    }
    
    if(exitflag==0){
        FILE *file;
        char flname[50];
        
        strcpy(flname,P1);
        
        if (exitflag==0&&(file = fopen(P1, "r")) != NULL)
        {
        printf("createvfs_FAILURE ");
         printf(ERR_VFS_CREATE_01);
        fclose(file);
        exitflag=1;
        printf("\n");
        }
        if(exitflag==0 && isdigit(flname[0])==1  )
        {
        printf("createvfs_FAILURE ");
        printf(ERR_VFS_CREATE_03);
        printf("\n");
        exitflag=1;
        }
        if(exitflag==0)
        {

            while(flname[i]!='\0' && exitflag==0)
            {    b=flname[i];
                if(b>32 && b<127 && b!=47)
                i++;
                else
                {
                exitflag=1;
                printf("createvfs_FAILURE ");
                printf(ERR_VFS_CREATE_03);
                exitflag=1;
                printf("\n");
                }
            }
        }
        if(exitflag==0 && P2<1 )
        {
        printf("createvfs_FAILURE ");
        printf(ERR_VFS_CREATE_04);
        exitflag=1;
        printf("\n");
        }
        if(P2>1024 && exitflag==0)
        {
        printf("createvfs_FAILURE ");
        printf(ERR_VFS_CREATE_04);
        exitflag=1;
        printf("\n");
        }
        if (exitflag==0)
        {
            if((file = fopen(P1, "w")) == NULL)
            {
            printf("createvfs_FAILURE ");
             printf(ERR_VFS_CREATE_02);
            exitflag=1;
            printf("\n");
            }
        }
        
        

            if(exitflag==0)
            {
            createFileVfs(P1,P2);
            printf("createvfs_SUCCESS\n");
            
            }
        
        
        
    }
}
void mountvfs ( char *P1 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("mountvfs_TO_BE_DONE\n");
	int arg1=strcmp(P1,"");
	int exitflag=0;
	if(P1==0)
	{
			printf("mountvfs_FAILURE ");
		 	printf(ERR_VFS_MOUNT_00);
			exitflag=1;
			printf("\n");
	}
	
	strcpy(flsname,P1);
	FILE *file;
	if (exitflag==0 && (file = fopen(P1, "r")) == NULL)
	{
	printf("mountvfs_FAILURE ");
 	printf(ERR_VFS_MOUNT_01);
	exitflag=1;
	printf("\n");
	}
	if(exitflag==0 && mount_flag==1)
	{
	printf("mountvfs_FAILURE ");
 	printf(ERR_VFS_MOUNT_03);
	exitflag=1;
	printf("\n");
	}
	if(exitflag==0 && mount_flag==0)
	{
	root=narraymount(P1);
	mount_flag=1;
	strcpy(vfsname,P1);
	printf("mountvfs_SUCCESS ");
	printf("\n");
	}
}
	

void unmountvfs ( char *P1 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("unmountvfs_TO_BE_DONE\n");
	
	int arg1=strcmp(P1,"");
	int exitflag=0;
	if(arg1==0)
	{
			printf("unmountvfs_FAILURE ");
		 	printf(ERR_VFS_UNMOUNT_00);
			exitflag=1;
			printf("\n");
	}

	

	FILE *file;
	if (exitflag==0 && (file = fopen(P1, "r")) == NULL)
	{
	printf("unmountvfs_FAILURE ");
 	printf(ERR_VFS_UNMOUNT_01);
	printf("\n");
	exitflag=1;
	}
	if (exitflag==0)	
	{
	int arg2=strcmp(vfsname,P1);
	//printf("vfsnme: %s arg2: %d\n",vfsname,arg2);
	
		if(arg2!=0)
		{
		printf("unmountvfs_FAILURE ");
	 	printf(ERR_VFS_UNMOUNT_03);
		printf("\n");
		exitflag=1;
		}
	}
	
	if (exitflag==0 && mount_flag==1)	
	{
	//displayList();
	flushBst();
	root1=NULL;
	//displayList();

	//printHash();
	flushHash();
	//printHash();
	
	reinitializefd(P1);
	count=0;
	unmount_narry(P1,root);
	
	mount_flag=0;
	printf("unmountvfs_SUCCESS ");
	printf("\n");
	//displayfiles(vfsname);
	}
}

void makedir ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("makedir_TO_BE_DONE\n");

	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int exitflag=0,a,b,i;
	if(mount_flag != 1)
	{
	printf("makedir_FAILURE ");
	printf(ERR_VFS_MAKEDIR_05);
	exitflag=1;
	printf("\n");	
	}
	if(exitflag==0) 
	{
	if(arg1==0 || arg2==0)
	{
	printf("makedir_FAILURE ");
	printf(ERR_VFS_MAKEDIR_00);
	exitflag=1;
	printf("\n");
	}
	}
	if(exitflag==0) 
	{
	 if(fd_count<=0)
	{
	printf("makedir_FAILURE ");
	printf(ERR_VFS_MAKEDIR_01);
	exitflag=1;
	printf("\n");
	}
	}
	/*else if(isdigit(P2[0])==1)
	{
		printf("makedir_FAILURE ");
		printf(ERR_VFS_MAKEDIR_02);
		printf("\n");
		exitflag=1;
	}f
	*/
	if(exitflag==0)
	{
		while(P2[i]!='\0' && exitflag==0)
		{
		b=P2[i];
		if(b>32 && b<127 && b!=47)
		i++;
		else
		{
		exitflag=1;
		printf("makedir_FAILURE ");
		printf(ERR_VFS_MAKEDIR_02);;
		printf("\n");
		}
		}
	}
	if(exitflag==0)
	{
		//printf("in else\n");
		char temploc[100];
		strcpy(temploc,P1);
		if(strcmp(temploc,"/")==0)
		strcat(temploc,"");
		else
		strcat(temploc,"/");
		strcat(temploc,P2);
		a=searchBst(temploc);
		if(a==1)
		{
		printf("makedir_FAILURE ");
		printf(ERR_VFS_MAKEDIR_03);
		exitflag=1;
		printf("\n");
		}
	}
		if(exitflag==0)
		{
		int success=makedircall(P2,P1,root);
		if(success==0)
		{
		printf("makedir_FAILURE ");
		printf(ERR_VFS_MAKEDIR_01);
		exitflag=1;
		printf("\n");
		}
		}
		if(exitflag==0)
		{
		//displayList();
		//printfolder(root);
		printf("makedir_SUCCESS\n");
		}
	//printfolder(root);
}

void deletedir ( char *P1 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("deletedir_TO_BE_DONE\n");
	int exitflag=0,a;
	if(mount_flag != 1)
	{
	printf("deletedir_FAILURE ");
	printf(ERR_VFS_DELETEDIR_04);
	exitflag=1;
	printf("\n");	
	}

	int arg1=strcmp(P1,"");
	
	if(exitflag==0)
	{
		if(arg1==0)
		{
		printf("deletedir_FAILURE ");
		printf(ERR_VFS_DELETEDIR_00);
		exitflag=1;
		printf("\n");
		}
	}
	
	char temploc[100];
	strcpy(temploc,P1);
	
	if(exitflag==0 && strcmp(temploc,"/")==0)
	{
		printf("deletedir_FAILURE ");
		printf(ERR_VFS_DELETEDIR_05);
		exitflag=1;
		printf("\n");
	}
	if(exitflag==0)
	{
		a=searchBst(temploc);
		//printf("Temploc %s P1 %s a %d\n",temploc,P1,a);
	
		if(a==0)
		{
		printf("deletedir_FAILURE ");
		printf(ERR_VFS_DELETEDIR_01);
		printf("\n");
		exitflag=1;
		}
		else
		{
		
		
		root=deleteChild(P1,root);
		
		
		}
	}

	
}

void movedir ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("movedir_TO_BE_DONE\n");
	char temploc[50],srcloc[50],dstloc[50],fname[20];
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	//printf("%d %d\n",arg1,arg2);
	//printf("%s %s\n",P1,P2);
	int exitflag=0,a,b,c,i;

	strcpy(srcloc,P1);
	strcpy(dstloc,P2);

	if(mount_flag != 1)
	{
	printf("movedir_FAILURE ");
	printf(ERR_VFS_MOVEDIR_08);
	printf("\n");	
	exitflag=1;
	}
	if(exitflag==0)
	{
		if(arg1==0 || arg2==0)
		{
		printf("movedir_FAILURE ");
		printf(ERR_VFS_MOVEDIR_00);
		printf("\n");
		exitflag=1;
		}
	}	
	
	a=searchBst(P1);
	if(strcmp(P2,"/")==0)
	b=1;
	else
	b=searchBst(P2);

	if(exitflag==0 && a==0)
	{
		
		printf("movedir_FAILURE ");
		printf(ERR_VFS_MOVEDIR_01);
		printf("\n");
		exitflag=1;

	}
	if(exitflag==0 && b==0)
	{
		
		printf("movedir_FAILURE ");
		printf(ERR_VFS_MOVEDIR_02);
		printf("\n");
		exitflag=1;

	}

	if(exitflag==0)
	{
			char * result =strtok(srcloc,"/");
			while(result!=NULL)
			{
				strcpy(fname,result);

				result=strtok(NULL,"/");		
		
			}
			//printf("FNAME:%s\n",fname);
			strcpy(temploc,dstloc);
			strcat(temploc,"/");
			strcat(temploc,fname);
		c=searchBst(temploc);
		if(c==1)
		{
		printf("movedir_FAILURE ");
		printf(ERR_VFS_MOVEDIR_05);
		exitflag=1;
		printf("\n");
		}
	}
	
	if(exitflag==0)
	{
	int strsz=strlen(P1);
	int i,j;
	char temploc[50];
	for(i=0;i<strsz;i++)
	{
	temploc[i]=P2[i];
	}
	j=strcmp(P1,temploc);
	if(j==0)
	{
		printf("movedir_FAILURE ");
		printf(ERR_VFS_MOVEDIR_06);
		exitflag=1;
		printf("\n");
	}
	}
	if(exitflag==0)
	{
		
		//printf("%s %s\n",P1,P2);
		
		root=movedircall(P1,P2,root);
		printf("movedir_SUCCESS\n");
	}
	
	
}

void listdir ( char *P1, int P2, char *P3 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("listdir_TO_BE_DONE\n");
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P3,"");
	int exitflag=0,chk_flag=0,a,i;
	struct fileDescriptor *ldfd;
	int flag=P2;
	

	if(exitflag==0)
	{
		 if(arg1==0 || arg2==0 || P3==NULL)
		{
		printf("listdir_FAILURE  ");
		printf(ERR_VFS_LISTDIR_00);
		printf("\n");
		exitflag=1;
		}
	}
	if(mount_flag != 1)
	{
	printf("listdir_FAILURE ");
	printf(ERR_VFS_LISTDIR_03);
	printf("\n");	
	exitflag=1;
	}
	
	if(flag==0 || flag==1)
	chk_flag=1;

	if(exitflag==0 && chk_flag!=1)
	{
		printf("listdir_FAILURE  ");
		printf(ERR_VFS_LISTDIR_02);
		exitflag=1;
		printf("\n");
		
		
	}

	a=searchBst(P1);
	if(a==1)
	{
	ldfd=searchBstfd(P1);
	if(strcmp(ldfd->fileType,"dir")!=0)
	{
	a=0;
	}
	}

	if(strcmp(P1,"/")==0)
	a=1;

	if(exitflag==0 && a==0)
	{

	
		printf("listdir_FAILURE  ");
		printf(ERR_VFS_LISTDIR_01);
		printf("\n");
		exitflag=1;
		
		
	}


	
		if(exitflag==0)
		{
		int ret;
		ret=listdir_new(P1, P2,P3,root);
		if(ret==0)
		printf("listdir_SUCCESS \n");
		}
	

	
}

void addfile ( char *P1, char *P2, char *P3 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("addfile_TO_BE_DONE\n");
	
	FILE *file;
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int arg3=strcmp(P3,"");
	int exitflag=0,a,b,c,d,i,g;
	char P22[20];
	strcpy(P22,P2);
//------------------------------------------------------------
	char delims[] = ".";
	char *result;
	char fnamenew[20], fileType[10],tempft[10];
	int templen;
	if(mount_flag != 1)
	{
	printf("addfile_FAILURE ");
	printf(ERR_VFS_ADDFILE_07);
	printf("\n");
	exitflag=1;	
	}
	
	
	
	if(exitflag==0)
	{
	result = strtok( P22, delims );
	if( result != NULL ) 
	{
   	 //printf( "result is \"%s\"\n", result );
    	strcpy(fnamenew,result);
	//printf("fnamenew : %s\n",fnamenew);
    	result = strtok( NULL, delims );
	if(result!=NULL)
	{
		strcpy(fileType,result);
		strcpy(fnamenew,P2);
		//printf("if block\n");
		//printf("fnamenew : %s\n",P2);
		strcpy(tempft,fileType);
		//----------------------------
		templen=strlen(fileType);
		for(i=0;i<templen;i++)
		fileType[i]=tolower(tempft[i]);
		
		//----------------------------
		c=strcmp(fileType,"txt");
		d=strcmp(fileType,"text");

			if(c==0 || d==0)
			strcpy(fileType,"text");
			else
			strcpy(fileType,"Non-text");
	}
	else
	{
		strcpy(fileType,"text");
		//strcat(fnamenew,".");
		//strcat(fnamenew,fileType);
		//printf("else block\n");
		strcpy(P2,fnamenew);
		//printf("fnamenew : %s\n",P2);
	}

	}

	}
//----------------------------------------------------------------

	if(exitflag==0)
	{
	if(arg1==0 || arg2==0 || arg3==0)
	{
	printf("addfile_FAILURE ");
	printf(ERR_VFS_ADDFILE_00);
	printf("\n");
	exitflag=1;
	}
	}
	if(exitflag==0)
	{
	if((file = fopen(P3, "r")) == NULL)
	{
	printf("addfile_FAILURE ");
 	printf(ERR_VFS_ADDFILE_08);
	printf("\n");
	exitflag=1;
	}
	}
	if(exitflag==0)
	{
	if(fd_count<=0)
	{
	printf("addfile_FAILURE ");
	printf(ERR_VFS_ADDFILE_04);
	printf("\n");
	exitflag=1;
	}
	}
	/*else if(isdigit(P2[0])==1)
	{
		printf("addfile_FAILURE ");
		printf(ERR_VFS_ADDFILE_02);
		printf("\n");
		exitflag=1;
	}
	*/
	
	if(exitflag==0)
	{	i=0;
		while(P2[i]!='\0' && exitflag==0)
		{
		b=P2[i];
		if(b>32 && b<127 && b!=47)
		i++;
		else
		{
		exitflag=1;
		printf("addfile_FAILURE ");
		printf(ERR_VFS_ADDFILE_03);
		printf("\n");
		exitflag=1;
		}
		}
	}
	char temploc[100];
		strcpy(temploc,P1);
		if(strcmp(temploc,"/")==0)
		strcat(temploc,"");
		else
		strcat(temploc,"/");
		strcat(temploc,P2);
		a=searchBst(temploc);
	if(exitflag==0 && a==1)
	{
		exitflag=1;
		printf("addfile_FAILURE ");
		printf(ERR_VFS_ADDFILE_03);
		printf("\n");
	}
	if(exitflag==0)
	{
	if(strcmp(P1,"/")!=NULL)
	g=searchBst(P1);
	else
	g=1;
	if(g==0)
	{
	printf("addfile_FAILURE ");
	printf(ERR_VFS_ADDFILE_09);
	printf("\n");
	exitflag=1;
	}
	}

	if(exitflag==0)
	{
	
	struct fileDescriptor *fd1;
	//fd1=addDataFile(P1,P2,P3);
	fd1=addDataFile(P1,P2,fileType,P3);
				if(fd1==NULL)
				{
					//printf("in if\n");
				}
				else
				{
					//printf("type:%s\n",fd1->fileType);
					//printf("size:%d\n",fd1->fileSize);
					//root=insertChild(P2,P1,root,fd1);	
					
					root=insertChild(P2,fileType,P1,root,fd1);
					printf("addfile_SUCCESS\n");
					//printfolder(root);
					
					
				}
	}	
}

void listfile ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int exitflag=0,a,i;
	if(mount_flag != 1)
	{
	printf("listdir_FAILURE ");
	printf(ERR_VFS_LISTFILE_04);
	printf("\n");	
	exitflag=1;
	}
	if(exitflag==0)
	{
	 if(arg1==0 || arg2==0)
	{
	printf("listfile_FAILURE  ");
	printf(ERR_VFS_LISTFILE_00);
	printf("\n");
	exitflag=1;
	}
	}
	a=searchBst(P1);
	if(exitflag==0 && a==0)
	{
	printf("listfile_FAILURE  ");
	printf(ERR_VFS_LISTFILE_01);
	printf("\n");
	exitflag=1;
	}
	if(exitflag==0)
	{
	int ret;
	//printf("listfile_TO_BE_DONE\n");
	ret=listfile_new(P1,P2,vfsname,root);
	if(ret==0)
	printf("listfile_SUCCESS\n");
	}
	
	
}

void updatefile ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("updatefile_TO_BE_DONE\n");
	FILE *file;
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int exitflag=0,a,i;
	if(mount_flag != 1)
	{
	printf("updatefile_FAILURE ");
	printf(ERR_VFS_COPYFILE_05);
	printf("\n");	
	exitflag=1;
	}
	if(exitflag==0)
	{
	 if(arg1==0 || arg2==0)
	{
	printf("updatefile_FAILURE ");
	printf(ERR_VFS_COPYFILE_00);
	exitflag=1;
	printf("\n");
	}
	}
	if(exitflag==0)
	{
	 if((file = fopen(P2, "r")) == NULL)
	{
	printf("updatefile_FAILURE ");
 	printf(ERR_VFS_UPDATEFILE_02);
	exitflag=1;
	printf("\n");
	}
	}
	if(exitflag==0)
	{
	 int x=updateDataFile(P1,P2);
	if(x!=0)
	printf("updatefile_SUCCESS\n");
	}
}

void removefile ( char *P1 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("removefile_TO_BE_DONE\n");

	
	int arg1=strcmp(P1,"");
	int exitflag=0,a;
	struct fileDescriptor *flfd;

	if(mount_flag != 1)
	{
	printf("removefile_FAILURE ");
	printf(ERR_VFS_REMOVEFILE_02);
	printf("\n");	
	exitflag=1;
	}
	if(exitflag==0 && arg1==0)
	{
	printf("removefile_FAILURE ");
	printf(ERR_VFS_REMOVEFILE_00);
	printf("\n");
	exitflag=1;
	}	
	
	a=searchBst(P1);
	if(a==1)
	{
	flfd=searchBstfd(P1);
	if(strcmp(flfd->fileType,"dir")==0)
	{
	a=0;
	}
}
	
	if(exitflag==0 && a==0)
	{
		
		printf("removefile_FAILURE ");
		printf(ERR_VFS_REMOVEFILE_01);
		printf("\n");
		exitflag=1;

	}
	
	if(exitflag==0)
	{
				
		root=deleteChild(P1,root);
		if(root!=NULL)
		printf("removefile_SUCCESS\n");
		

	}
	

	
}

void movefile ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("movefile_TO_BE_DONE\n");
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int exitflag=0,a,b,c,i,aa,bb,dest_type=0;
	struct fileDescriptor *a_mvflfd,*b_mvflfd;
	if(mount_flag != 1)
	{
	printf("movefile_FAILURE ");
	printf(ERR_VFS_MOVEFILE_06);
	printf("\n");	
	int exitflag=1;
	}
	if(exitflag==0 &&(arg1==0 || arg2==0))
	{
	printf("movefile_FAILURE ");
	printf(ERR_VFS_MOVEFILE_00);
	printf("\n");
	int exitflag=1;
	}	
	
	a=searchBst(P1);
	if(a==1)
	{
	a_mvflfd=searchBstfd(P1);

	if(strcmp(a_mvflfd->fileType,"dir")==0)
	a=0;
	}

	if(strcmp(P2,"/")==0)
	{b=1;dest_type=1;}
	else
	{
	b=searchBst(P2);
		if(b==1)
		{
		b_mvflfd=searchBstfd(P2);
		if(strcmp(b_mvflfd->fileType,"dir")==0)
		dest_type=1;
		}
	}
	
	if(exitflag==0 && a==0)
	{
		
		printf("movefile_FAILURE ");
		printf(ERR_VFS_MOVEFILE_01);
		printf("\n");
		exitflag=1;

	}
	if(exitflag==0 && b==0)
	{
		
		printf("movefile_FAILURE ");
		printf(ERR_VFS_MOVEFILE_02);
		printf("\n");
		exitflag=1;

	}
	if(exitflag==0)
	{
		if(b==1 && dest_type==1)
		root=movefilecall(P1,P2,root);			
		else
		root=movefilecallnew(P1,P2,root);
		printf("movefile_SUCCESS\n");
		

	}
	
	
	


}

void copyfile ( char *P1, char *P2 )
{
	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int exitflag=0,a,i;
	if(mount_flag != 1)
	{
	printf("copyfile_FAILURE ");
	printf(ERR_VFS_COPYFILE_05);
	printf("\n");
	exitflag==1;	
	}
	if(exitflag==0)
	{
	if(arg1==0 || arg2==0)
	{
	printf("copyfile_FAILURE ");
	printf(ERR_VFS_COPYFILE_00);
	printf("\n");
	exitflag==1;
	}
	}
	if(exitflag==0 && fd_count<=0)
	{
	printf("copyfile_FAILURE ");
	printf(ERR_VFS_COPYFILE_04);
	printf("\n");
	exitflag==1;
	}
	if(exitflag==0)
	{
		int x1,j;
		char newstr1[100];
		char str[100];
		strcpy(str,P2);
		//printf("hello");
		x1=0;
	count=0;
	while(str[x1]!='\0')
	{
	if(str[x1]=='/')
	count=x1;
	x1++;
	}
	//count++;
	j=0;
	while(j<count)
	{
		newstr1[j]=str[j];
		//count++;
	j++;
	}
	newstr1[j]='\0';
	int x2=searchBst(newstr1);
	//printf("%s",newstr1);
	if(x2==0)
	{
		printf("copyfile_FAILURE ");
	printf(ERR_VFS_COPYFILE_02);
	printf("\n");
	exitflag=1;
	}
	
	}
	if(exitflag==0)
	{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("copyfile_TO_BE_DONE\n");
	struct fileDescriptor *fd1;
	fd1=copyfile1(P1,P2);

				if(fd1==NULL)
				{
					//printfolder(root);
					//displayList();	
				}
				else
				{
					//------------------------------------------------------------------------
					//split the location path for inserting into narry
						
					/*int lp_length,cn_length,diff_length,i;	//location path length, childname length

					lp_length=strlen(fd1->locationPath);
					cn_length=strlen(fd1->fileName);		
	
					diff_length=lp_length-(cn_length+1);
	
					char updated_locpath[diff_length+1];

					for(i=0;i<diff_length;i++){
					updated_locpath[i]=fd1->locationPath[i];
					}
		
					updated_locpath[i]='\0';*/
					int i,j,count;
					char str[100],updated_locpath[100],newstr[100];
					strcpy(str,fd1->locationPath);
					i=0;
					count=0;
					while(str[i]!='\0')
					{
					if(str[i]=='/')
					count=i;
					i++;
					}
					//count++;
					j=0;
					while(j<count)
					{
						newstr[j]=fd1->locationPath[j];
						j++;
					}
					newstr[j]='\0';
					//printf("new loc:%s\n",newstr);
					strcpy(updated_locpath,newstr);					
					//------------------------------------------------------------------------
					//printf("Updated loc :%s\n",updated_locpath);
					//printf("fileName in vfs:%s\n",fd1->fileName);
					//printf("fileName in vfs:%s\n",fd1->locationPath);
					//printf("type:%s\n",fd1->fileType);
					//printf("size:%d\n",fd1->fileSize);
					//root=insertChild(fd1->fileName,updated_locpath,root,fd1);
					root=insertChild(fd1->fileName,fd1->fileType,updated_locpath,root,fd1);	
					//printfolder(root);
					//displayList();
					printf("copyfile_SUCCESS\n");
					
				}	
	}

	
}

void exportfile ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("exportfile_TO_BE_DONE\n");
	FILE *fs,*ft;
	char outputFileName[100],srcfilelocation[100];
	strcpy(outputFileName,P2);
	strcpy(srcfilelocation,P1);


	if( strlen(srcfilelocation)== 0 || strlen(outputFileName)==0)
	{	
		//ERR_VFS_EXPORTFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"
		printf("exportfile_FAILURE");
		printf(ERR_VFS_EXPORTFILE_00);
		printf("\n");
		
	}
	else if (mount_flag == 0)
	{	
		//ERR_VFS_EXPORTFILE_04 "VFS_NOT_MOUNTED"
		printf("exportfile_FAILURE");
		printf(ERR_VFS_EXPORTFILE_04);
		printf("\n");
	}
	
	else
		exportfile1(P1,P2);
}

void searchfile ( char *P1, char *P2 )
{
	/* Call the appropriate function with given arguments and display appropriate output on the screen */
	//printf("searchfile_TO_BE_DONE\n");

	int arg1=strcmp(P1,"");
	int arg2=strcmp(P2,"");
	int exitflag=0,a,i;
	if(mount_flag != 1)
	{
	printf("searchfile_FAILURE ");
	printf(ERR_VFS_SEARCHFILE_02);
	printf("\n");	
	exitflag=1;
	}
	else if(arg1==0 || arg2==0)
	{
	printf("searchfile_FAILURE ");
	printf(ERR_VFS_SEARCHFILE_00);
	printf("\n");
	exitflag=1;
	}
	
	if(exitflag==0)
	{
	int flag=searchFile(P1,P2);
	printf("searchfile_SUCCESS\n");
	}
	/*int flag=searchFile(P1,P2);
	if(flag==1)
	{
	//printf(ERR_VFS_SEARCHFILE_01);
	printf("\n");
	}
	else 
	{
	FILE *fp;
	fp=fopen(P2,"r");
	if (fp==NULL)
	{
	printf(ERR_VFS_SEARCHFILE_02);
	printf("\n");
	}
	fclose(fp);
	}
	*/
}

void printbst ()
{
		
displayList();
}

void printhash ()
{
	
printHash();
}

